<template>
  <el-container style="padding: 15px">
    <el-header height="80px">
      <el-form size="small" label-width="80px">
        <el-row type="flex" align="center">
          <el-col :span="4">
            <el-form-item label="不动产类型" label-width="90px">
              <el-select value="">
                <el-option value="" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="项目编号">
              <el-input />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="项目名称">
              <el-input />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="用途">
              <el-select value="">
                <el-option value="" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="估价目的">
              <el-select value="">
                <el-option value="" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" align="center" justify="center">
          <el-col :span="4">
            <el-button type="primary" size="small">搜索</el-button>
            <el-button type="info" size="small">重置</el-button>
            <el-button size="small" :loading="downloadButtonLoading" @click="downloadBySearch">下载</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-header>
    <el-divider />
    <el-main>
      <el-form size="small">
        <el-row type="flex">
          <el-col :span="4">
            <el-form-item>
              <!--              <el-button type="warning" @click="tableHeadSetDialogVisible = true">表头设置</el-button>-->
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table :data="reportList">
        <el-table-column v-for="item in dynamicTableHeadList" :key="item.prop" :label="item.label" :prop="item.prop" :show-header="item.showHead" width="120px" />
      </el-table>
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 40, 80]"
        :page-size="sizePage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalPage"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-main>
    <el-dialog
      title="表头设置"
      :visible.sync="tableHeadSetDialogVisible"
      width="50%"
      :before-close="tableHeadSetDialogHandlerClose"
    >
      <el-checkbox-group v-model="setDynamicHeadList">
        <el-checkbox v-for="item in dynamicTableHeadList" :key="item" :label="item.label" :checked="item.showHead" @change="setHeadHandler" />
      </el-checkbox-group>
      <el-divider />
      <el-button type="primary" size="small">重置</el-button>
      <el-button size="small">清空</el-button>
    </el-dialog>
  </el-container>
</template>

<script>
import { downloadStatistics, getDynamicTableHead, getStatisticsPage } from '@/api/statistics'
import { validObj } from '@/utils/validate'
import { mapGetters } from 'vuex'

export default {
  name: 'MyStatistics',
  data() {
    return {
      dynamicTableHeadList: [],
      setDynamicHeadList: [],
      tableHeadSetDialogVisible: false,
      reportList: [],

      currentPage: 1,
      sizePage: 10,
      totalPage: 0,

      searchObj: {},

      downloadButtonLoading: false
    }
  },

  created() {
    this.init()
  },

  // eslint-disable-next-line vue/order-in-components
  computed: {
    ...mapGetters([
      'id'
    ])
  },

  methods: {
    init() {
      this.getTableHead()
      this.getList()
    },
    handleSizeChange(val) {
      this.sizePage = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },
    getList() {
      const currentPage = this.currentPage
      const sizePage = this.sizePage
      const searchObj = this.searchObj
      const id = this.id
      if (validObj(searchObj)) {
        // 搜索
      } else {
        getStatisticsPage(id, currentPage, sizePage).then(res => {
          this.reportList = res.data.list
          this.totalPage = res.data.total
        })
      }
    },
    getTableHead() {
      getDynamicTableHead().then(res => {
        this.dynamicTableHeadList = res.data.head
      })
    },

    tableHeadSetDialogHandlerClose() {
      this.tableHeadSetDialogVisible = false
    },

    setHeadHandler(val) {
      console.log(val)
    },

    downloadBySearch() {
      const searchObj = this.searchObj
      this.downloadButtonLoading = true
      downloadStatistics(searchObj).then(res => {
        const url = window.URL.createObjectURL(new Blob([res], { type: res.type }))
        const link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', new Date().getTime() + '.xlsx')
        document.body.appendChild(link)
        link.click()
        this.downloadButtonLoading = false
      })
    }
  }
}
</script>

<style scoped>

</style>
